﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;
using System.Collections.Generic;

namespace UI
{
    /// <summary>
    /// 竞技场夺宝悬赏任务……
    /// </summary>
    public class UIActivityBattleRace : UIActivityTemplate
    {
        public UILabel topTips;
        public GameObject vsGetItem;
        public GameObject multiItem;
        public UILabel activityTime;
        public SimpleGrid simpleGrid;
        public UIScrollView scrollView;
        public GameObject[] boys;
        public GameObject[] bgs;
        public ActivityType type;
        int[] canGet = new int[6];
        GameObject itemPref;
        public override void OnCoverPageRemove()
        {
            base.OnCoverPageRemove();
        }
        protected override void PrepareData()
        {
            switch (type)
            {
                case ActivityType.BattleRace:
                    SetBgAndPerson(0);
                    topTips.text = TableTextBlo.GetText("BattleRace_award_activity_tip");
                    break;
                case ActivityType.RobTrea:
                    SetBgAndPerson(1);
                    topTips.text = TableTextBlo.GetText("RobTrea_award_activity_tip");
                    break;
                case ActivityType.PrizeTask:
                    SetBgAndPerson(2);
                    topTips.text = TableTextBlo.GetText("PrizeTask_award_activity_tip");
                    break;
                case ActivityType.PrizeBoss:
                    SetBgAndPerson(3);
                    topTips.text = TableTextBlo.GetText("PrizeBoss_award_activity_tip");
                    break;
                case ActivityType.MainCopy:
                    SetBgAndPerson(1);
                    topTips.text = TableTextBlo.GetText("MainCopy_award_activity_tip");
                    break;
                case ActivityType.AincradStars:
                    SetBgAndPerson(2);
                    topTips.text = TableTextBlo.GetText("Practice_award_rmb_tip");
                    break;
            }
            activityType = type;
        }

        void SetBgAndPerson(int flag)
        {
            for(int i = 0;i < bgs.Length; i++)
            {
                bgs[i].gameObject.SetActive(false);
                boys[i].gameObject.SetActive(false);
            }
            bgs[flag].gameObject.SetActive(true);
            boys[flag].gameObject.SetActive(true);
        }

        public void SetRedByNum()
        {
            switch (type)
            {
                case ActivityType.BattleRace:
                    if (canGet[0] < 1)
                    {
                        StaticData.redTips.SetPointState(RedPointTip.Red.pvpWinTimes, 0);
                        StaticData.redTips.ReduceActivitys();
                    }
                    break;
                case ActivityType.RobTrea:
                    if (canGet[1] < 1)
                    {
                        StaticData.redTips.SetPointState(RedPointTip.Red.robTreasureTimes, 0);
                        StaticData.redTips.ReduceActivitys();
                    }
                    break;
                case ActivityType.PrizeTask:
                    if (canGet[2] < 1)
                    {
                        StaticData.redTips.SetPointState(RedPointTip.Red.fightRedNameTimes, 0);
                        StaticData.redTips.ReduceActivitys();
                    }
                    break;
                case ActivityType.PrizeBoss:
                    if (canGet[3] < 1)
                    {
                        StaticData.redTips.SetPointState(RedPointTip.Red.killRedNameTimes, 0);
                        StaticData.redTips.ReduceActivitys();
                    }
                    break;
                case ActivityType.MainCopy:
                    if (canGet[4] < 1)
                    {
                        StaticData.redTips.SetPointState(RedPointTip.Red.mainCopyWinTimes, 0);
                        StaticData.redTips.ReduceActivitys();
                    }
                    break; 
                case ActivityType.AincradStars:
                    if (canGet[5] < 1)
                    {
                        StaticData.redTips.SetPointState(RedPointTip.Red.aincradStars, 0);
                        StaticData.redTips.ReduceActivitys();
                    }
                    break;
            }
        }

        public void AddCanGetNum()
        {
            switch (type)
            {
                case ActivityType.BattleRace:
                    canGet[0]++;
                    break;
                case ActivityType.RobTrea:
                    canGet[1]++;
                    break;
                case ActivityType.PrizeTask:
                    canGet[2]++;
                    break;
                case ActivityType.PrizeBoss:
                    canGet[3]++;
                    break;
                case ActivityType.MainCopy:
                    canGet[4]++;
                    break;
                case ActivityType.AincradStars:
                    canGet[5]++;
                    break;
            }
        }

        public void ResetCanGetNum()
        {
            for (int i = 0; i < canGet.Length; i++)
                canGet[i] = 0;
        }

        public override void Show()
        {
            base.Show();
            CSActivityInfo msg = new CSActivityInfo();
            msg.activityId = this.activityId;
            NetAPI.SendHttp(OpDefine.CSActivityInfo, msg, MsgCallback);
        }
        void MsgCallback(NetWork.Packets.Packet packet, bool success)
        {
            if (success)
            {
                SCActivityInfo msg = packet.kBody as SCActivityInfo;

                if (msg.itemList.Count <= 0)
                {
                    Debug.Log("活动列表为0");
                    return;
                }
                activityId = msg.activityId;
                //时间
                System.DateTime endTime = ServerTimerTool.Java2CSTimeLocale(msg.endTime);
                System.DateTime startTime = ServerTimerTool.Java2CSTimeLocale(msg.startTime);
                string endTimeStr = endTime.Month.ToString() + TableTextBlo.GetText("month") + endTime.Day.ToString() + TableTextBlo.GetText("day_2")
                    + string.Format("{0:d2}:{1:d2}:{2:d2}", endTime.Hour, endTime.Minute, endTime.Second);
                string startTimeStr = startTime.Month.ToString() + TableTextBlo.GetText("month") + startTime.Day.ToString() + TableTextBlo.GetText("day_2")
                    + string.Format("{0:d2}:{1:d2}:{2:d2}", startTime.Hour, startTime.Minute, startTime.Second);
                string acTime = startTimeStr + TableTextBlo.GetText("to") + endTimeStr;
                if (msg.param == "1")
                    activityTime.text = string.Format(TableTextBlo.GetText("activity_time_Format"), acTime);
                else
                {
                    System.DateTime deadTime = ServerTimerTool.Java2CSTimeLocale(msg.rewardEndTime);
                    acTime = deadTime.Month.ToString() + TableTextBlo.GetText("month") + deadTime.Day.ToString() + TableTextBlo.GetText("day_2")
                    + string.Format("{0:d2}:{1:d2}:{2:d2}", deadTime.Hour, deadTime.Minute, deadTime.Second);
                    activityTime.text = string.Format(TableTextBlo.GetText("getReward_DeadTime"), acTime);
                }
                UpdateVsList(msg.itemList);
            }
        }

        void UpdateVsList(List<ActivityItem> list)
        {
            foreach (var item in simpleGrid.childs)
            {
                Destroy(item);
            }
            simpleGrid.childs.Clear();
            ResetCanGetNum();
            for (int i = 0; i < list.Count; i++)
            {
                ActivityItem item = list[i];
                if (item.rewardList.Count > 1)
                {
                    itemPref = multiItem;
                    GameObject obj = Instantiate(itemPref);
                    obj.SetActive(true);
                    simpleGrid.AddChild(obj); 
                    var script = obj.GetComponent<VsGetItem>();
                    script.m_update = UpdateVsList;
                    script.type = type;
                    script.m_addNum = AddCanGetNum;
                    script.SetActivityMultiItem(item, activityId);
                }
                else
                {
                    itemPref = vsGetItem;
                    GameObject obj = Instantiate(itemPref);
                    obj.SetActive(true);
                    simpleGrid.AddChild(obj);
                    var script = obj.GetComponent<VsGetItem>();
                    script.m_update = UpdateVsList;
                    script.type = type;
                    script.m_addNum = AddCanGetNum;
                    script.SetActivityItem(item, activityId);
                }
            }
            multiItem.SetActive(false);
            vsGetItem.SetActive(false);
            SetRedByNum();
            if (list.Count < 3)
                scrollView.enabled = false;
            else
                scrollView.enabled = true;
            //scrollView.ResetPosition();
        }
    }
}

